package com.xw.util;

/**
 * @author XW
 * @data 2023/8/3 13:53
 * @description生成链表
 */
public class GenerateLinkedListUtils {

    public static class Node{
        public int value;
        public Node next;

        public Node(int data){
            value = data;
        }
    }

    /**
     * 双链表
     */
    public static class DoubleNode {
        public int value;
        public DoubleNode last;
        public DoubleNode next;

        public DoubleNode(int data) {
            value = data;
        }
    }

    /**
     *  生成长度和最大值随机的单链表
     * @param len 最大长度
     * @param value 最大值
     * @return
     */
    public static Node generateRandomLinkedList(int len,int value){
        int size = (int)(Math.random() * (len+1));
        if(size == 0){
            return null;
        }
        size --;
        Node head = new Node((int)(Math.random()*(value +1)));
        Node pre = head;
        while(size != 0){
            Node cur = new Node((int)(Math.random()*(value +1)));
            pre.next = cur;
            pre = cur;
            size--;
        }
        return head;
    }

    /**
     * 生成长度和最大值随机的单链表
     * @param len
     * @param value
     * @return
     */
    public static DoubleNode generateRandomDoubleLinkedList(int len,int value){
        int size = (int)(Math.random() * (len+1));
        if(size == 0){
            return null;
        }
        size --;
        DoubleNode head = new DoubleNode((int)(Math.random()*(value +1)));
        DoubleNode pre = head;
        while(size != 0){
            DoubleNode cur = new DoubleNode((int)(Math.random()*(value +1)));
            pre.next = cur;
            cur.last = pre;
            pre = cur;
            size--;
        }
        return pre;
    }
}
